package com.wsq.nutz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;





import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import com.wsq.nutz.bean.Role;
import com.wsq.nutz.bean.User;
import com.wsq.nutz.service.AuthorityService;
import com.wsq.nutz.service.UserService;

public class MainSetup implements Setup{

	@Override
	public void init(NutConfig conf) {
/*		// 初始化数据库表
        Ioc ioc = conf.getIoc();
        Dao dao = ioc.get(Dao.class);
        // 如果没有createTablesInPackage,请检查nutz版本
        Daos.createTablesInPackage(dao, "net.wendal.nutzbook", false);
        
        
        // 初始化默认根用户
        if (dao.count(User.class) == 0) {
            User user = new User();
            user.setName("admin");
            user.setPassword("123456");
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            dao.insert(user);
        }*/
		
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        Ioc ioc = conf.getIoc();
        Dao dao = ioc.get(Dao.class);
        // 如果没有createTablesInPackage,请检查nutz版本
        Daos.createTablesInPackage(dao, "com.wsq.nutz", false);
        //List<User> users = (List<User>) conf.getLoadingContext().get("userList");
        // 初始化默认根用户
        //if (users == null || users.isEmpty()) {
            List<User> users = new ArrayList<User>();
            UserService us = ioc.get(UserService.class);
            
            User user1 = us.add(1,"jack", "456");
            List<Role>roles1 = new ArrayList<Role>();
            Role r1 = new Role();
            r1.setId(1);
            r1.setName("admin");
            roles1.add(r1);
            user1.setRoles(roles1);
            
            
            User user2 = us.add(2,"wsq", "123");
            List<Role>roles2 = new ArrayList<Role>();
            Role r2 = new Role();
            r2.setId(1);
            r2.setName("guest");
            roles2.add(r2);
            user2.setRoles(roles2);
            
            users.add(user1);
            users.add(user2);
        //}
        
        conf.getServletContext().setAttribute("users", users);
        
/*        AuthorityService as = ioc.get(AuthorityService.class);
        as.initFormPackage("com.wsq.nutz");
        as.checkBasicRoles(us.add(3,"admin", "123"));*/
	}

	@Override
	public void destroy(NutConfig nc) {
		
	}
}
